package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.DepartmentMapper;
import com.usian.mapper.EmployeeMapper;
import com.usian.mapper.PostMapper;
import com.usian.pojos.Department;
import com.usian.pojos.Employee;
import com.usian.pojos.Post;
import com.usian.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService {

    /**
     * 查询员工信息
     * @return
     */
    @Override
    public List<Employee> findEmployee(Employee employee) {

        List<Employee> list = new ArrayList<>();

        if (employee.getQuanxian()=="3"){

            QueryWrapper<Employee> wrapper = new QueryWrapper<>();

            wrapper.lambda().eq(Employee::getQuanxian,"3").eq(Employee::getDid,employee.getDid());

            list=list(wrapper);

            return list;
            }

        list = list();

        return list;
    }

    /**
     * 添加员工信息
     * @param employee
     */
    @Override
    public void addEmployee(Employee employee) {

        String s = DigestUtils.md5DigestAsHex((employee.getPassword()+"123456").getBytes());

        employee.setPassword(s);

        save(employee);
    }

    /**
     * 修改员工信息
     * @param employee
     */
    @Override
    public void updateEmployee(Employee employee) {

        updateById(employee);
    }

    /**
     * 删除员工信息
     * @param id
     */
    @Override
    public void delEmployee(Integer id) {

        removeById(id);
    }

    /**
     * 登录
     * @param employee
     */
    @Override
    public int login(Employee employee) {

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Employee::getEname,employee.getEname());

        Employee one = getOne(wrapper);

        if (one == null){

            return 1;
        }
        String s = DigestUtils.md5DigestAsHex((employee.getPassword()+one.getSalt()).getBytes());

        if (s.equals(one.getPassword())){

            return 0;
        }

        return 1;
    }

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findDList() {

        return departmentMapper.selectList(null);
    }

    @Override
    public List<Post> findPList() {

        return postMapper.selectList(null);
    }
}
